﻿#ifndef TCPCLIENT_H
#define TCPCLIENT_H


#include "QTcpSocket"
#include "netManger_tcp.h"
#include "netManger_udp.h"
//#include "QElapsedTimer"  //代码计时器
#include "QTimer"


class TcpClient : public QObject
{
    Q_OBJECT
public:
    std::function<void()> Fun_ClearList = nullptr;
    std::function<void(QString name, bool isTcp, uint16_t serPort, uint16_t localPort, bool persistent)> Fun_addItem = nullptr;
    std::function<void()> Fun_disAppConnect = nullptr;

private:
    QTcpSocket* socket = nullptr;
    QTimer* pantTimer = nullptr;
    QString usrID;

    netManger_tcp* tcpManger = nullptr; //TCP管理器
    netManger_udp* udpManger = nullptr; //UDP管理器

    uint8_t noResponseNum = 0; //无响应次数

public:
    explicit TcpClient();
    ~TcpClient();

    bool isValid(); //判断是否有效
    bool connectServer(QHostAddress host,uint16_t port,QString id = "Default"); //连接服务器
    bool addProxy(QString name, bool isTcp, uint16_t listenPort, uint16_t proxyPort, bool persistent = false); //远程添加映射

    bool getProxyList(); //获取映射列表
    void removeProxy(uint16_t proxyPort); //移除映射

    void OnTcpReadyRead(); //客户端TCP收到数据
    void OnTcpDisConnect(); //客户端断开连接


    void OnLocalTcpClientReadyRead(QTcpSocket* socket); //本地TCP客户端收到数据
    void OnLocalTcpClientDisconnect(QTcpSocket* socket); //本地TCP客户端断开连接

    void OnLocalUdpClientReadyRead(QString netHost,uint16_t netPort,uint16_t proxyPort,QByteArray data); //本地UDP客户端收到数据，转发给服务器
};

#endif // TCPCLIENT_H
